import 'package:aweb5/generated/json/jjhw_product_list_entity_helper.dart';
import 'package:aweb5/util/DialogUtil.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:aweb5/util/NaviUtil.dart';
import 'package:aweb5/style.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:aweb5/custom/pull_torefresh.dart';
import 'package:load/load.dart';
import 'package:aweb5/config/Config.dart';
import 'package:aweb5/Constants.dart';
import 'package:aweb5/util/toast_util.dart';
import 'package:aweb5/bean/aplus/jjhw_product_list_entity.dart';
import 'package:aweb5/util/HttpClient.dart';

class ProductList extends StatefulWidget {
  Function linkcall;
  Function call;
  bool link;
  ProductList({this.call, this.linkcall, this.link});
  @override
  _ProductListState createState() => _ProductListState();
}

class _ProductListState extends State<ProductList> {
  TextEditingController textEditingController = TextEditingController();
  String search = "";

  RefreshController refreshController = RefreshController();
  List<JjhwProductListData> data = List();

  String id = "";
  String type = "1"; //1秒杀 2商品 1. 商品连接 3. 秒杀连接
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {

      requestProductList();

    });

  }

  JjhwProductListData productListData;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: ScreenUtil().setHeight(650),
        padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(16),
            right: ScreenUtil().setWidth(11),
            top: ScreenUtil().setHeight(20)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                GestureDetector(
                  child: Container(
                    width: ScreenUtil().setWidth(55),
                    height: ScreenUtil().setHeight(55),
                    padding: EdgeInsets.only(
                      right: ScreenUtil().setWidth(26),
//                      bottom: ScreenUtil().setHeight(26)
                    ),
                    child: Image.asset(
                      "assets/assist/guanbib.png",
                      width: ScreenUtil().setWidth(24),
                      height: ScreenUtil().setHeight(24),
                    ),
                  ),
                  onTap: () {
                    NaviUtil.pop(this.context);
                  },
                ),
                GestureDetector(
                  child: Container(
                    alignment: Alignment.center,
                    width: ScreenUtil().setWidth(94),
                    height: ScreenUtil().setHeight(40),
                    decoration: redButtonStyleWithBorder,
                    child: Text(
                      "确认选择",
                      style: TextStyle(
                          color: Color(0xffF94F68),
                          fontSize: ScreenUtil().setSp(14)),
                    ),
                  ),
                  onTap: () {
                    if (widget.link) {
                      widget.linkcall(this.productListData.id.toString(), this.type,this.productListData.name,this.productListData.imgUrl);
                    } else {
                      if (widget.call != null) {
                        widget.call(this.productListData);
                      }
                    }
                    NaviUtil.pop(this.context);
                  },
                )
              ],
            ),
            Container(
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(381),
              height: ScreenUtil().setHeight(39),
              decoration: BoxDecoration(
                  color: Color(0xfff1f1f1),
                  borderRadius: BorderRadius.circular(5)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SizedBox(
                    width: ScreenUtil().setWidth(20),
                  ),
                  Expanded(
                      child: TextField(
                    controller: textEditingController,
                    decoration: InputDecoration(
                        hintText: "输入团品或关键字搜索",
                        hintStyle: TextStyle(color: Color(0xffa3a3a3)),
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(bottom: 12)),
                  )),
                  GestureDetector(
                    child: Image.asset(
                      "assets/search1.png",
                      width: ScreenUtil().setWidth(22),
                      height: ScreenUtil().setHeight(22),
                    ),
                    onTap: () {
                      search = textEditingController.text;
                      if(this.type=="1"){
                        requestProductList();
                      }else if(this.type=="3"){
                        requestMiaoshaList();
                      }
                    },
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(18),
                  )
                ],
              ),
            ),
            SizedBox(
              height: ScreenUtil().setHeight(18),
            ),
            Row(
              children: <Widget>[
                SizedBox(
                  width: ScreenUtil().setWidth(10),
                ),
                GestureDetector(
                  child: Container(
                    width: ScreenUtil().setWidth(179),
                    height: ScreenUtil().setHeight(40),
                    alignment: Alignment.center,
                    decoration: this.type=="1"?redButtonStyle:redButtonStyleWithBorder,
                    child: Text(
                      "商城团品",
                      style: TextStyle(
                          color: this.type=="1"?Colors.white:Color(0xffF94F68),
                          fontSize: ScreenUtil().setSp(16)),
                    ),
                  ),
                  onTap: () {
                    this.type = "1";
                    setState(() {

                    });
                    requestProductList();
                  },
                ),
                Expanded(
                  child: Text(""),
                ),
                GestureDetector(
                  child: Container(
                    width: ScreenUtil().setWidth(179),
                    height: ScreenUtil().setHeight(40),
                    alignment: Alignment.center,
                    decoration: this.type=="3"?redButtonStyle:redButtonStyleWithBorder,
                    child: Text(
                      "秒杀商品",
                      style: TextStyle(
                          color: this.type=="3"?Colors.white:Color(0xffF94F68),
                          fontSize: ScreenUtil().setSp(16)),
                    ),
                  ),
                  onTap: () {
                    this.type = "3";
                    setState(() {

                    });
                    requestMiaoshaList();
                  },
                ),
                SizedBox(
                  width: ScreenUtil().setWidth(10),
                ),
              ],
            ),
            SizedBox(
              height: ScreenUtil().setHeight(18),
            ),
            Expanded(
                child: PullToRefresh().buildRefresh(
                    refresh: this.refresh,
                    load: this.load,
                    child: CustomScrollView(
                      slivers: <Widget>[
//                      SliverToBoxAdapter(
//                        child: Container(
//                          child: Text(
//                            "素材库",
//                            style: b(),
//                          ),
//                          margin: EdgeInsets.only(
//                              bottom: ScreenUtil().setHeight(20)),
//                        ),
//                      ),
//                      SliverGrid(
//                        delegate: SliverChildBuilderDelegate(
//                          (BuildContext context, int index) {
//                            return sheetDialogItem();
//                          },
//                          childCount: 10,
//                        ),
//                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//                          mainAxisSpacing: 10, // 主轴间距
//                          crossAxisSpacing: 10, // 纵轴间距
//                          crossAxisCount: 3, // 纵轴子元素个数
//                          childAspectRatio: 0.7,
//                        ),
//                      ),
//                        SliverToBoxAdapter(
//                          child: Container(
//                            child: Text(
//                              "任务包",
//                              style: b(),
//                            ),
//                            margin: EdgeInsets.only(
//                                bottom: ScreenUtil().setHeight(20)),
//                          ),
//                        ),
                        SliverGrid(
                          delegate: SliverChildBuilderDelegate(
                            (BuildContext context, int index) {
                              return sheetDialogItem(index);
                            },
                            childCount: data.length,
                          ),
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                            mainAxisSpacing: 10, // 主轴间距
                            crossAxisSpacing: 10, // 纵轴间距
                            crossAxisCount: 3, // 纵轴子元素个数
                            childAspectRatio: 0.9,
                          ),
                        ),
                      ],
                    ),
                    pulldown: false,
                    pullup: true,
                    refreshController: refreshController))
          ],
        ),
      ),
    );
  }

  void refresh() {}

  void load() {}

  Widget sheetDialogItem(int index) {
    return GestureDetector(
      child: Stack(
        children: <Widget>[
          Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "assets/assist/wenjian1.png",
                  width: ScreenUtil().setWidth(80),
                  height: ScreenUtil().setHeight(80),
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(6),
                ),
                Text(
                  "${data[index].name}",
                  style: b1(),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          )
          ,
          Positioned(
            child: GestureDetector(
              child: Image.asset(
                   "assets/assist/xuanze.png"
                       ,
                  width: ScreenUtil().setWidth(21),
                  height: ScreenUtil().setHeight(data[index].select
                      ?21:0)),
              onTap: () {
                setState(() {});
              },
            ),
            top: 0,
            right: 0,
          )
        ],
      ),
      onTap: () {
        for (int i = 0; i < data.length; i++) {
          data[i].select = false;
        }
        data[index].select = true;
        this.productListData = data[index];
//        this.id = data[index].id.toString();

//          widget.call();

        setState(() {});
      },
    );
  }


  TextStyle b1() {
    return TextStyle(
        color: Color(0xff101010), fontSize: ScreenUtil().setSp(14));
  }

  TextStyle grey() {
    return TextStyle(
        color: Color(0xffa7a7a7), fontSize: ScreenUtil().setSp(14));
  }
//商品
  void requestProductList() async {
    DialogUtil.showloadingdialog(context);
    try {
      Map<String, dynamic> map = Map<String, dynamic>();

//      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["keyword"] = textEditingController.text.toString();

      Map<String, dynamic> map1 = await HttpClient.requestLogin(
          Config.requestApi + 'light/task/getGoods',
          data: map);

      JjhwProductListEntity loginEntity = new JjhwProductListEntity();

      loginEntity = jjhwProductListEntityFromJson(loginEntity, map1);

      DialogUtil.pop(context);
      if (loginEntity.code == null || loginEntity.code != 200) {
//        Fluttertoast.showToast(msg: "null");
        Toast.show(msg: loginEntity.msg);

        return;
      }
      if (loginEntity.code == 200) {
//        Toast.show(msg: "创建成功");
//        NaviUtil.pop(this.context);
        data = loginEntity.data;


        setState(() {});
      }
    } catch (e) {
      DialogUtil.pop(context);
      print(e.toString());

    }
  }

  //秒杀

  void requestMiaoshaList() async {
    DialogUtil.showloadingdialog(context);
    try {
      Map<String, dynamic> map = Map<String, dynamic>();

//      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["keyword"] = textEditingController.text.toString();
      Map<String, dynamic> map1 = await HttpClient.requestLogin(
          Config.requestApi + 'light/task/getSeckill',
          data: map);

      JjhwProductListEntity loginEntity = new JjhwProductListEntity();

      loginEntity = jjhwProductListEntityFromJson(loginEntity, map1);

      DialogUtil.pop(context);
      if (loginEntity.code == null || loginEntity.code != 200) {
//        Fluttertoast.showToast(msg: "null");
        Toast.show(msg: loginEntity.msg);

        return;
      }
      if (loginEntity.code == 200) {
//        Toast.show(msg: "创建成功");
//        NaviUtil.pop(this.context);
        data = loginEntity.data;


        setState(() {});
      }
    } catch (e) {
      DialogUtil.pop(context);
      print(e.toString());

    }
  }

}
